﻿#pragma execution_character_set("utf-8")

#include "MainWindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QVariant>
#include <QStackedWidget>
#include <QToolButton>
#include <QDebug>
#include <QFontDatabase>
#include "QEventViewForm.h"
#include "MainPageForm.h"
MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    setProperty("canMove",true);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    initWindowTitle("电力网络态势感知系统V2.0");
    this->setMinimumSize(1200,768);
}

MainWindow::~MainWindow()
{

}

void MainWindow::initWindowTitle(QString strTitle)
{
    this->setProperty("form",true);
    //新建标题栏，LOGON ,文字 ， 主按钮1，方按钮2，主按钮3，系统控制按钮
    QWidget * topWidget = new QWidget(this);
   // topWidget->setMinimumHeight(100);
    QHBoxLayout * topWidgetLayout = new QHBoxLayout(topWidget);
    topWidgetLayout->setSpacing(0);
    topWidgetLayout->setMargin(0);
    topWidget->setProperty("form","title");
    //Logo
    QFont ft;
    ft.setPixelSize(30);
    QLabel * lbLogo = new QLabel(this);
    lbLogo->setText(QChar(0xf073));

    //title
    QLabel * lbTitle = new QLabel(this);
    lbTitle->setText(strTitle);
    lbTitle->setFont(QFont("Microsoft Yahei", 20));

    topWidgetLayout->addWidget(lbLogo);
    topWidgetLayout->addWidget(lbTitle);
    //按钮数组1
    QSize icoSize(48, 48);
    int icoWidth = 95;

    QStringList listTitle = {QString("主界面"),QString("参数配置"),QString("事件查看"),QString("帮助系统"),QString("退出系统")};
    QStringList iconAddr={":/image/main_main.png",
                          ":/image/main_config.png",
                          ":/image/main_data.png",
                          ":/image/main_help.png",
                          ":/image/main_exit.png"};

    btnWidget = new QWidget(this);

    QHBoxLayout * btnLayout = new QHBoxLayout(btnWidget);
    for(int i = 0 ; i < listTitle.size() ;i++)
    {
        QIcon icon;
        icon.addFile(iconAddr[i]);
        QToolButton * btn = new QToolButton(this);
        btn->setIcon(icon);
        btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        btn->setText(listTitle[i]);
        btn->setIconSize(icoSize);
        btn->setMinimumWidth(icoWidth);
        btn->setCheckable(true);
        if(i==0)   btn->click();
        connect(btn,SIGNAL(clicked()),this,SLOT(topNavPushButtonClick()));
        btnLayout->addWidget(btn);

    }
    btnLayout->setMargin(0);
    btnLayout->setSpacing(0);
    btnWidget->setProperty("nav","top");
    topWidgetLayout->addWidget(btnWidget);
    topWidgetLayout->addStretch();

    //系统控制菜单
    QStringList btnSysControl =  {QChar(0xf068),QChar(0xf067),QChar(0xf00d)};
    QHBoxLayout * hLayoutSysControl = new QHBoxLayout();

    int fontId = QFontDatabase::addApplicationFont(":/image/fontawesome-webfont.ttf");
    QStringList fontName = QFontDatabase::applicationFontFamilies(fontId);
    QFont iconFont;
    if (fontName.count() > 0) {
        iconFont = QFont(fontName.at(0));
    } else {
        qDebug() << "load fontawesome-webfont.ttf error";
    }

    for(int i = 0 ; i < btnSysControl.size() ;i++)
    {
        QPushButton * btn = new QPushButton(this);
        iconFont.setPixelSize(14);
        btn->setFont(iconFont);
        btn->setText(btnSysControl[i]);
        //btn->setIconSize(icoSize);
        btn->setMinimumWidth(50);
        btn->setMinimumHeight(80);
        btn->setProperty("type",i+1);
        connect(btn,SIGNAL(clicked()),this,SLOT(topNavPushButtonClick()));
        hLayoutSysControl->addWidget(btn);

    }
    QVBoxLayout * vLayoutSysControl = new QVBoxLayout();
    vLayoutSysControl->addItem(hLayoutSysControl);
    vLayoutSysControl->addStretch(0);
    vLayoutSysControl->setStretch(1,2);
    topWidgetLayout->addItem(vLayoutSysControl);

    //整个界面的主布局器，垂直布局，上面为系统控制菜单，下面为页面选项卡
    QVBoxLayout * mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(topWidget);


    //选项卡，一级主界面切换
    m_stackedWidget = new QStackedWidget();
    mainLayout->addWidget(m_stackedWidget);

   // m_stackedWidget->setwi
    MainPageForm * mainForm = new MainPageForm();
    m_stackedWidget->addWidget(mainForm);
    QEventViewForm * eventForm = new QEventViewForm();
    m_stackedWidget->addWidget(eventForm);


    mainLayout->setStretch(1,2);
    mainLayout->setSpacing(0);
    mainLayout->setMargin(0);
}
void MainWindow::topNavPushButtonClick()
{
    qDebug()<<"page count = "<<m_stackedWidget->count();
    QToolButton * b = ((QToolButton *)sender());

    QList<QToolButton *> tbtns = btnWidget->findChildren<QToolButton *>();
    foreach (QToolButton *btn, tbtns) {
                btn->setChecked(false);
    }

    b->setChecked(true);
    if(b->text()==QString("主界面"))
    {
        m_stackedWidget->setCurrentIndex(0);
    }
    if(b->text()==QString("事件查看"))
    {
        m_stackedWidget->setCurrentIndex(1);
    }

    if(((QToolButton *)sender())->text()==QString("退出系统"))
        exit(0);
}
